package com.entfrm.biz.it.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.biz.it.entity.Property;
import com.entfrm.biz.it.entity.PropertyCategory;
import com.entfrm.biz.it.excel.PropertyExcel;
import com.entfrm.biz.it.service.PropertyCategoryService;
import com.entfrm.biz.it.service.PropertyService;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.constant.CommonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <h3>entfrm</h3>
 * <p>资产分类</p>
 *
 * @author : kyc
 * @date : 2020-10-14 08:49
 **/
@Component
public class PropertyCategoryManager {

    @Autowired
    private PropertyCategoryService propertyCategoryService;

    @Autowired
    private PropertyService propertyService;

    public R removeById(Integer categoryId) {
        List<PropertyCategory> categoryList = propertyCategoryService.list();
        for (PropertyCategory propertyCategory : categoryList) {
            if (categoryId.equals(propertyCategory.getParentId())) {
                return R.error("删除失败,请先删除子分类!");
            }
        }
        List<Property> propertyList = propertyService.list();
        for (Property property : propertyList) {
            if (property.getCategoryId() != null && property.getCategoryId().equals(categoryId)) {
                return R.error("该分类已被使用,无法删除!");
            }
        }
        return R.ok(propertyCategoryService.removeById(categoryId));
    }
}
